package com.pccw.portlet.util;

public class CharacterChangeUtil {
	
	public static String code(String s) {
		String unicode = "";
		char[] charary = new char[s.length()];
		for (int i = 0; i < charary.length; i++) {
			charary[i] = (char) s.charAt(i);
			unicode += "\\u" + Integer.toString(charary[i], 16);
		}
		return unicode;
	}

	public static String recode(String code) {
		StringBuffer buf=null;
		try {
			String[] unicode = code.split("\\\\u"); // 第一个是空白字符，忽略
			buf = new StringBuffer(unicode.length - 1);
			for (int i = 1; i < unicode.length; i++){
				buf.append((char) Integer.parseInt(unicode[i], 16));
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
		return buf.toString();
	}
	
	public static void main(String[] args){
		String abc=code("你好");
		System.out.println(abc);
		System.out.println(recode(abc));
	}
}
